use std::sync::Arc;

use crate::data_channels::{
    command::execute_command_request,
    message::{MessageCommand, MessageType},
    proxy::{
        local_execute_batch_script_message_request, local_execute_cli_message_request,
        local_execute_http_request,
    },
    request::put_response,
    ui_request,
};

use super::message::Message;

pub async fn on_message(peer_id: &String, message: Message) -> anyhow::Result<Option<Message>> {
    //
    tracing::debug!("{}:{}", peer_id, message.data.len());

    let message_type = &message.r#type;

    match message_type {
        MessageType::RESPONSE => {
            tracing::debug!("接收到响应消息{}::{}", peer_id, message.data.len());
            let command = &message.command;
            match command {
                MessageCommand::HTTP => {
                    if let Err(err) = put_response(message).await {
                        //
                        tracing::debug!("写入响应失败!{}", err);
                    }
                }
                _ => {
                    if let Err(err) = put_response(message).await {
                        //
                        tracing::debug!("写入响应失败!{}", err);
                    }
                }
            }
        }
        MessageType::REQUEST => {
            tracing::debug!("接收到请求消息{}::{}", peer_id, message.data.len());
            //
            let command = &message.command;
            match command {
                MessageCommand::PING => return Ok(Some(Message::pone(message.request_id))),
                MessageCommand::HTTP => {
                    //http的请求
                    let request_id = message.request_id;

                    let response_result = local_execute_http_request(message).await;
                    match response_result {
                        Ok(mut response_message) => {
                            response_message.request_id = request_id;

                            tracing::debug!("处理HTTP成功{}::{}", peer_id, request_id);
                            return Ok(Some(response_message));
                        }
                        Err(err) => {
                            return Ok(Some(Message::error_with_request_id(
                                request_id,
                                &format!("处理HTTP请求失败!{}", err,),
                                MessageCommand::HTTP,
                            )));
                        }
                    }
                }
                MessageCommand::CLI => {
                    //http的请求
                    let request_id = message.request_id;

                    let response_result = local_execute_cli_message_request(message).await;
                    match response_result {
                        Ok(mut response_message) => {
                            response_message.request_id = request_id;

                            tracing::debug!("处理CLI成功{}::{}", peer_id, request_id);
                            return Ok(Some(response_message));
                        }
                        Err(err) => {
                            return Ok(Some(Message::error_with_request_id(
                                request_id,
                                &format!("处理CLI请求失败!{}", err,),
                                MessageCommand::CLI,
                            )));
                        }
                    }
                }
                MessageCommand::BATCHSCRIPT => {
                    //http的请求
                    let request_id = message.request_id;

                    let response_result = local_execute_batch_script_message_request(message).await;
                    match response_result {
                        Ok(mut response_message) => {
                            response_message.request_id = request_id;

                            tracing::debug!("处理BATCH SCRIPT成功{}::{}", peer_id, request_id);
                            return Ok(Some(response_message));
                        }
                        Err(err) => {
                            return Ok(Some(Message::error_with_request_id(
                                request_id,
                                &format!("处理BATCH SCRIPT请求失败!{}", err,),
                                MessageCommand::CLI,
                            )));
                        }
                    }
                }
                MessageCommand::PUBSUB => {
                    tracing::debug!("接收到PUBSUB请求消息{}::{}", peer_id, message.data.len());
                    if let Err(err) =
                        crate::pubsub::on_data_channel_message(peer_id, message.data).await
                    {
                        tracing::error!("处理PUBSUB失败:{}", err);
                        return Ok(None);
                    } else {
                        return Ok(None);
                    }
                }
                MessageCommand::UICOMMAND => {
                    tracing::debug!("接收到UICOMMAND请求消息{}::{}", peer_id, message.data.len());
                    let request_id = message.request_id;

                    let response_result = execute_command_request(peer_id, message, 60000).await;
                    match response_result {
                        Ok(mut response_message) => {
                            response_message.request_id = request_id;

                            tracing::debug!("处理UICOMMANDP成功{}::{}", peer_id, request_id);
                            return Ok(Some(response_message));
                        }
                        Err(err) => {
                            tracing::debug!("处理UICOMMANDP失败{}::{}", request_id, err);
                            return Ok(Some(Message::error_with_request_id(
                                request_id,
                                &format!("处理UICOMMAND请求失败!{}", err,),
                                MessageCommand::UICOMMAND,
                            )));
                        }
                    }
                }
                _ => {
                    tracing::debug!("没有匹配到命令处理{}::{}", peer_id, message.request_id);
                }
            }
        }
        MessageType::ERROR => {
            //是错误的响应
            tracing::debug!("MessageType::ERROR{}::{}", peer_id, message.request_id);
            let command = &message.command;
            let request_id = message.request_id;
            match command {
                MessageCommand::HTTP => {}
                MessageCommand::UICOMMAND => {}
                _ => {}
            }
            if request_id > 0 {
                if let Err(err) = put_response(message).await {
                    //
                    tracing::debug!("写入错误的响应失败!{}", err);
                }
            }
        }
        MessageType::EMPTY => {
            tracing::debug!("MessageType::EMPTY{}::{}", peer_id, message.request_id);
        }
    }
    //
    Ok(None)
}
